import { Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { Post } from './model/post.model';
import { InjectModel } from '@nestjs/mongoose';
import CreatePostDto from './dto/create-post.dto';
import UpdatePostDto from './dto/update-post.dto';

@Injectable()
export class PostsService {
  constructor(
    @InjectModel(Post.name) private readonly postModel: Model<Post>,
  ) {}

  createPost(newPost: CreatePostDto): Promise<Post> {
    return this.postModel.create(newPost);
  }

  deletePost(delPostId: string) {
    return this.postModel.deleteOne({
      _id: delPostId,
    });
  }

  updatePost(toBeUpdatedPost: UpdatePostDto, id: string) {
    return this.postModel.findByIdAndUpdate({ _id: id }, toBeUpdatedPost);
  }

  findOnePost(toBeFoundPost: string) {
    return this.postModel.findOne({ title: toBeFoundPost });
  }

  findAllPost() {
    return this.postModel.find();
  }
}
